﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblCommPositionDAL:ImtblCommPosition
    {
        HKEA_CMSEntities model;
        mtblCommPosition position;
        public mtblCommPositionDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblCommPositionDAL GetInstance()
        {
            return new mtblCommPositionDAL();
        }
        public int InsertENT(mtblCommPosition ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblCommPositions.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblCommPosition> GetListByENT(mtblCommPosition ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblCommPositions.Where(a => a.CommPositionUID > 0);
            if (ent.CommPositionUID > 0)
                result = result.Where(a => a.CommPositionUID == ent.CommPositionUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblCommPosition> GetListByENT(mtblCommPosition ent)
        {
            var result = model.mtblCommPositions.Where(a => a.CommPositionUID > 0);
            if (ent.CommPositionUID > 0)
                result = result.Where(a => a.CommPositionUID == ent.CommPositionUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }


        public int GetCountByENT(mtblCommPosition ent)
        {
            var result = model.mtblCommPositions.Where(a => a.CommPositionUID > 0);
            if (ent.CommPositionUID > 0)
                result = result.Where(a => a.CommPositionUID == ent.CommPositionUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblCommPosition ent)
        {
            position = model.mtblCommPositions.Where(a => a.CommPositionUID == ent.CommPositionUID).FirstOrDefault();
            if (position != null)
            {
                position.CommPosition_Chi = ent.CommPosition_Chi;
                position.CommPosition_Eng = ent.CommPosition_Eng;
                position.Deleted = ent.Deleted;
                position.LastModifyBy = ent.LastModifyBy;
                position.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblCommPosition ent)
        {
            position = model.mtblCommPositions.Where(a => a.CommPositionUID == ent.CommPositionUID).FirstOrDefault();
            if (position != null)
            {
                model.mtblCommPositions.DeleteObject(position);
            }
            return model.SaveChanges();
        }
    }
}
